package global

import (
	"fmt"
	"github.com/astaxie/beego"
	"github.com/astaxie/beego/orm"
	"github.com/astaxie/beego/session"
	. "teamcoop/models"
)

type FormLogin struct {
	Email     string `form:"email",		valid:"Required;Email; MaxSize(64)"` // Email字段需要符合邮箱格式，并且最大长度不能大于100个字符
	Pwd       string `form:"pwd",		valid:"Required;Min(6)"`               // Pwd必须为足够长的密码
	AutoLogin string `form:"autoLogin"`
}
type FormRigster struct {
	Nick  string `form:"nick",	valid:"Required;Range(1,64)"`
	Email string `form:"email",	valid:"Required;Email; MaxSize(64)"` // Email字段需要符合邮箱格式，并且最大长度不能大于100个字符
	Pwd   string `form:"pwd",	valid:"Required;Min(6)"`               // Pwd必须为足够长的密码
}

var MySessions *session.Manager

func init() {
	MySessions, _ = session.NewManager("memory", `{
		"teamcoop_cookie":"teamcoop_sessionid",
		"enableSetCookie,omitempty": true,
		"gclifetime":3600,
		"maxLifetime": 3600, 
		"secure": false, 
		"sessionIDHashFunc": "sha1", 
		"sessionIDHashKey": "",
		"cookieLifeTime": 3600,
		"providerConfig": ""
		}`)
	go MySessions.GC()
}
func Int64ToStr(in int64) string {
	return fmt.Sprintf("%d", in)
}
func GetUserClassName(in int8) (out string) {

	switch in {
	case 0:
		{
			return "普通用户"
		}
	case 1:
		{
			return "管理员"
		}
	case 2:
		{
			return "超级管理员"
		}
	}
	if in > 2 {
		return "超级管理员"
	}
	return "普通用户"
}

func GetLevelHeadLevel(in int8) (out string) {
	if in > 5 {
		in = 5
	}
	if in < 0 {
		in = 0
	}
	var ret string = fmt.Sprintf("%d", 6-int64(in))
	return ret
}
func GetLevelHead(in int8) string {
	if in > 5 {
		in = 5
	}
	if in < 0 {
		in = 0
	}
	in = (6 - in)

	//out = "<h" + fmt.Sprintf("%d", in) + ">"

	switch in {
	case 6:
		{
			return ""
			break
		}
	case 5:
		{
			return "5BC0DE"
			break
		}
	case 4:
		{
			return "5CB85C"
			break
		}
	case 3:
		{
			return "428BCA"
			break
		}
	case 2:
		{
			return "F0AD4E"
			break
		}
	case 1:
		{
			return "D9534F"
			break
		}
	}
	return ""
}
func GetDynamicOwnerId(did int64) (out string) {
	out = "UNKNOWN"
	dynamic := &Dynamic{}
	o := orm.NewOrm()
	qs := o.QueryTable("dynamic")
	err := qs.Filter("Id", did).One(dynamic)
	var uid int64
	if dynamic.Type == TYPE_COMMENT {
		comment := &Comment{}
		qs = o.QueryTable("comment")
		err = qs.Filter("Id", dynamic.CommentId).One(comment)
		if err != nil {
			beego.Error("GetDynamicOwnerName: comment is not exsit!")
			return
		}
		uid = comment.UserId
	} else if dynamic.Type == TYPE_TODO {
		todo := &Todo{}
		qs = o.QueryTable("todo")
		err = qs.Filter("Id", dynamic.TodoId).One(todo)
		if err != nil {
			beego.Error("GetDynamicOwnerName: todo is not exsit!")
			return
		}
		uid = todo.UserId
	} else {
		beego.Error("GetDynamicOwnerName: dynamic.Type error:", dynamic.Type)
		return
	}
	out = string(uid)
	return
}
func GetDynamicOwnerName(did int64) (out string) {
	out = "UNKNOWN"
	user := &User{}
	dynamic := &Dynamic{}
	o := orm.NewOrm()
	qs := o.QueryTable("dynamic")
	err := qs.Filter("Id", did).One(dynamic)
	var uid int64
	if dynamic.Type == TYPE_COMMENT {
		comment := &Comment{}
		qs = o.QueryTable("comment")
		err = qs.Filter("Id", dynamic.CommentId).One(comment)
		if err != nil {
			beego.Error("GetDynamicOwnerName: comment is not exsit!")
			return
		}
		uid = comment.UserId
	} else if dynamic.Type == TYPE_TODO {
		todo := &Todo{}
		qs = o.QueryTable("todo")
		err = qs.Filter("Id", dynamic.TodoId).One(todo)
		if err != nil {
			beego.Error("GetDynamicOwnerName: todo is not exsit!")
			return
		}
		uid = todo.UserId
	} else {
		beego.Error("GetDynamicOwnerName: dynamic.Type error:", dynamic.Type)
		return
	}

	qs = o.QueryTable("user")
	err = qs.Filter("Id", uid).One(user)
	if err != nil {
		beego.Error("GetDynamicOwnerName: user is not exsit!")
		return
	}
	out = user.Nick
	return
}
func GetDynamicContent(did int64) (out string) {
	out = "NOTHING"
	//user := &User{}
	dynamic := &Dynamic{}
	o := orm.NewOrm()
	qs := o.QueryTable("dynamic")
	err := qs.Filter("Id", did).One(dynamic)
	//var uid int64
	if dynamic.Type == TYPE_COMMENT {
		comment := &Comment{}
		qs = o.QueryTable("comment")
		err = qs.Filter("Id", dynamic.CommentId).One(comment)
		if err != nil {
			beego.Error("GetDynamicOwnerName: comment is not exsit!")
			return
		}
		out = comment.Text
	} else if dynamic.Type == TYPE_TODO {
		todo := &Todo{}
		qs = o.QueryTable("todo")
		err = qs.Filter("Id", dynamic.TodoId).One(todo)
		if err != nil {
			beego.Error("GetDynamicOwnerName: todo is not exsit!")
			return
		}
		out = todo.Text
	} else {
		beego.Error("GetDynamicOwnerName: dynamic.Type error:", dynamic.Type)
		return
	}
	return
}
func GetDynamicTypeName(in int8) (out string) {
	if in == 1 {
		out = "TODO"
	} else if in == 2 {
		out = "评论"
	}
	return
}

func GetUserName(uid int64) (out string) {
	out = "UNKNOWN"
	user := &User{}
	o := orm.NewOrm()
	qs := o.QueryTable("user")
	err := qs.Filter("Id", uid).One(user)
	if err != nil {
		beego.Error("GetUserName:user is exsit!")
		return
	}
	out = user.Nick
	return
}
